package service;

import dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserService {

    public List<User> getUserList(User user){
        // 使用mybatis实现用户查询
        try {
            // 1、加载配置文件
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            // 2、创建核心类
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            // 3、创建会话
            SqlSession sqlSession = sqlSessionFactory.openSession();
            // 4、执行方法
            List<User> userList = sqlSession.getMapper(UserMapper.class).findAll(user);
            // 5、返回结果
            return userList;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public int count(){
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            int count = sqlSession.getMapper(UserMapper.class).count();
            return count;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    // 用户新增
    public int addUser(User user) {
        // 实现方法
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            int count = sqlSession.getMapper(UserMapper.class).addUser(user);
            // 事务处理 提交事务  增删改 这三种操作会修改表数据
            sqlSession.commit();
            return count;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public int deleteUser(int id) {
        // 实现方法
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            int count = sqlSession.getMapper(UserMapper.class).deleteUser(id);
            // 事务处理 提交事务  增删改 这三种操作会修改表数据
            sqlSession.commit();
            return count;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public User getUserById(int id) {
        // 使用mybatis实现用户查询
        try {
            // 1、加载配置文件
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            // 2、创建核心类
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            // 3、创建会话
            SqlSession sqlSession = sqlSessionFactory.openSession();
            // 4、执行方法
            User user = sqlSession.getMapper(UserMapper.class).getUserById(id);
            // 5、返回结果
            return user;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public int editUser(User user) {
        // 实现方法
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            int count = sqlSession.getMapper(UserMapper.class).editUser(user);
            // 事务处理 提交事务  增删改 这三种操作会修改表数据
            sqlSession.commit();
            return count;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
